package com.meida.boot.handler.app;

import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.security.OpenHelper;
import com.meida.module.pay.provider.service.PayBalanceLogService;
import com.meida.module.user.client.entity.AppUser;
import com.meida.module.user.provider.service.AppUserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * 我的邀请用户
 *
 * @author zyf
 */
@Component("shareUserListHandler")
public class ShareUserListHandler implements PageInterceptor<AppUser> {

    @Resource
    private PayBalanceLogService payBalanceLogService;

    @Resource
    private AppUserService userService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }


    @Override
    public void complete(CriteriaQuery<AppUser> cq, List<EntityMap> result, EntityMap extra) {
        Long userId = OpenHelper.getUserId();
        AppUser appUser = userService.getUserById(userId);
        //查询总收益
        BigDecimal totalAmount = payBalanceLogService.totalAmountByLogType(userId, 1);
        extra.put("totalAmount", totalAmount);
        String shareUrl = "https://api.hswlm.cn/api/common/register/" + Optional.ofNullable(appUser.getShareCode()).orElse("");
        extra.put("shareUrl", shareUrl);
        extra.put("shareTitle", "推荐注册");
        extra.put("shareDesc", "玩了么用户分享注册");
        Integer userCount = userService.countByInviterId(userId);
        extra.put("userCount", userCount);

    }

    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {
        Long userId = OpenHelper.getUserId();
        cq.eq(true, "inviterId", userId);
    }
}
